package cn.clouds234.jschdemo;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.security.KeyPairGenerator;

/**
 * 暂时无法使用。秘钥认证失败
 *  描述类的功能
 * @author wyf
 * @date 2023/10/30 17:33
 */
public class SSHKeyGenerator {
    public static void main(String[] args) throws Exception {
        // 生成密钥对
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        // 导出公钥
        byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
        String publicKeyContent = "ssh-rsa " + java.util.Base64.getEncoder().encodeToString(publicKeyBytes);

        // 将公钥添加到authorized_keys文件
        Files.write(Paths.get("F:\\data\\rsa/authorized_keys"), publicKeyContent.getBytes());


        System.out.println("Linux免密登录秘钥文件生成成功！");
        System.out.println("公钥已添加到authorized_keys文件");
        System.out.println("私钥保存为private_key.pem");
    }
}
